package cn.iocoder.yudao.module.member.service.usergastemplaterelation;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.member.controller.admin.usergastemplaterelation.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usergastemplaterelation.UserGasTemplateRelationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.usergastemplaterelation.UserGasTemplateRelationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 燃气户与安检模板关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserGasTemplateRelationServiceImpl implements UserGasTemplateRelationService {

    @Resource
    private UserGasTemplateRelationMapper userGasTemplateRelationMapper;

    @Override
    public Long createUserGasTemplateRelation(UserGasTemplateRelationSaveReqVO createReqVO) {
        // 插入
        UserGasTemplateRelationDO userGasTemplateRelation = BeanUtils.toBean(createReqVO, UserGasTemplateRelationDO.class);
        userGasTemplateRelationMapper.insert(userGasTemplateRelation);
        // 返回
        return userGasTemplateRelation.getId();
    }

    @Override
    public void updateUserGasTemplateRelation(UserGasTemplateRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateUserGasTemplateRelationExists(updateReqVO.getId());
        // 更新
        UserGasTemplateRelationDO updateObj = BeanUtils.toBean(updateReqVO, UserGasTemplateRelationDO.class);
        userGasTemplateRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserGasTemplateRelation(Long id) {
        // 校验存在
        validateUserGasTemplateRelationExists(id);
        // 删除
        userGasTemplateRelationMapper.deleteById(id);
    }

    private void validateUserGasTemplateRelationExists(Long id) {
        if (userGasTemplateRelationMapper.selectById(id) == null) {
            throw exception(USER_GAS_TEMPLATE_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public UserGasTemplateRelationDO getUserGasTemplateRelation(Long id) {
        return userGasTemplateRelationMapper.selectById(id);
    }

    @Override
    public PageResult<UserGasTemplateRelationDO> getUserGasTemplateRelationPage(UserGasTemplateRelationPageReqVO pageReqVO) {
        return userGasTemplateRelationMapper.selectPage(pageReqVO);
    }

}